//
//  AY_BookMyHeaderView.m
//  AiYue
//
//  Created by boWen on 2023/10/19.
//

#import "AY_BookMyHeaderView.h"
#import "AY_BookSubStackView.h"
#import "AY_BookSettingPage.h"
#import "AY_BookQianDPage.h"
#import "AY_BookDPage.h"

@interface AY_BookMyHeaderView ()
@property (nonatomic, strong) UIImageView * my_bgTopView;
@property (nonatomic, strong) UIImageView * my_txImageView;
@property (nonatomic, strong) UILabel * topLabel;
@property (nonatomic, strong) UILabel * downLabel;
@property (nonatomic, strong) UIButton * copBtn;

@property (nonatomic, strong) UIButton * settingBtn;
@property (nonatomic, strong) UIButton * changeColorBtn;


//书豆、金币、优惠券
@property (nonatomic, strong) UIView * invite_bgView;
@property (nonatomic, strong) UIStackView * stackView;
@property (nonatomic, strong) AY_BookSubStackView * bookDouziView;
@property (nonatomic, strong) AY_BookSubStackView * bookRmbView;
@property (nonatomic, strong) AY_BookSubStackView * bookQuanView;

@property (nonatomic, strong) UIButton * qdBtn;
@property (nonatomic, strong) UIView *huYanView;

@end


@implementation AY_BookMyHeaderView

- (instancetype)initWithFrame:(CGRect)frame {
    if([super initWithFrame:frame]) {
        [self initWithAllUI];
    }
    return self;
}


- (void)initWithAllUI {
    self.my_bgTopView = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_my_topBg.png") andBg:[UIColor clearColor] andRadius:0.0 andAddSubView:self];
    self.my_bgTopView.userInteractionEnabled = YES;
    [self.my_bgTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.equalTo(self);
        make.height.mas_offset(GK_STATUSBAR_NAVBAR_HEIGHT);
    }];
    
    self.settingBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"" andFont:YFont(10) andTitleColor:COLOR_Bg andBg:nil andImage:YImage(@"icon_my_setting.png") andBgImage:nil andTarget:self andAction:@selector(settingWithAction) andRadius:0 andAddSubView:self];
    [self.settingBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(GK_STATUSBAR_HEIGHT + YNUM(10));
        make.right.equalTo(self).offset(YNUM(-24));
        make.height.width.mas_equalTo(YNUM(24));
    }];
    
    AY_ReadManager *readManager = [AY_ReadManager sharedInstance];
    self.changeColorBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"" andFont:YFont(10) andTitleColor:COLOR_Bg andBg:nil andImage: readManager.isNight ? YImage(@"AY_mine_yejian") : YImage(@"icon_my_changeColor.png") andBgImage:nil andTarget:self andAction:@selector(changeColorWithAction:) andRadius:0 andAddSubView:self];
    [self.changeColorBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.settingBtn);
        make.right.equalTo(self.settingBtn.mas_left).offset(YNUM(-20));
        make.height.width.mas_equalTo(YNUM(24));
    }];
    
    self.my_txImageView = [AYTool createImageViewWithFrame:CGRectZero andImage:YImage(@"icon_my_tx.png") andBg:COLOR_Bg andRadius:YNUM(27) andAddSubView:self];
    [self.my_txImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.width.mas_offset(YNUM(54));
        make.left.equalTo(self).offset(YNUM(12));
        make.top.equalTo(self).offset(GK_STATUSBAR_NAVBAR_HEIGHT);
    }];
    
    self.topLabel = [AYTool createLabelWithFrame:CGRectZero andText:@"" andTextColor:COLOR(@"#010101") andBgColor:nil andFont:[UIFont boldSystemFontOfSize:YNUM(18)] andLines:1 andRadius:0 andAddSubView:self];
    [self.topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.my_txImageView).offset(YNUM(2));
        make.left.equalTo(self.my_txImageView.mas_right).offset(YNUM(12));
        make.height.mas_equalTo(YNUM(24));
    }];
    
    self.downLabel = [AYTool createLabelWithFrame:CGRectZero andText:@"" andTextColor:COLOR(@"#909090") andBgColor:nil andFont:[UIFont systemFontOfSize:YNUM(12)] andLines:1 andRadius:0 andAddSubView:self];
    [self.downLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.my_txImageView).offset(YNUM(-2));
        make.left.equalTo(self.topLabel);
        make.height.mas_equalTo(YNUM(24));
    }];
    
    self.copBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"复制" andFont:YFont(12) andTitleColor:COLOR(@"909090") andBg:nil andImage:nil andBgImage:nil andTarget:self andAction:@selector(copAction) andRadius:0 andAddSubView:self];
    [self.copBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.downLabel.mas_right).offset(YNUM(5));
        make.centerY.equalTo(self.downLabel);
    }];
    
    if (![AYMMKV getBoolForKey:ISSHENHE defaultValue:YES]) {
        self.invite_bgView = [AYTool createViewWithFrame:CGRectZero andBg:[UIColor whiteColor] andRadius:YNUM(8) andAddSubView:self];
        [self.invite_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.my_txImageView.mas_bottom).offset(YNUM(10));
            make.left.equalTo(self).offset(YNUM(16));
            make.right.equalTo(self).offset(YNUM(-16));
            make.height.mas_offset(YNUM(72));
        }];

        self.bookDouziView = [[AY_BookSubStackView alloc] initWithFrame:CGRectZero];
        self.bookDouziView.downLabel.text = @"书豆";
        self.bookDouziView.topLabel.text = @"0.00";
        
        self.bookRmbView = [[AY_BookSubStackView alloc] initWithFrame:CGRectZero];
        self.bookRmbView.downLabel.text = @"金币";
        self.bookRmbView.topLabel.text = @"0";
       
        
        self.bookQuanView = [[AY_BookSubStackView alloc] initWithFrame:CGRectZero];
        self.bookQuanView.downLabel.text = @"优惠券";
        self.bookQuanView.topLabel.text = @"0";
       
        
        self.stackView = [[UIStackView alloc] initWithArrangedSubviews:@[self.bookDouziView, self.bookRmbView, self.bookQuanView]];
        [self.invite_bgView addSubview:self.stackView];
        [self.stackView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.left.top.bottom.equalTo(self.invite_bgView);
        }];
        self.stackView.distribution = UIStackViewDistributionFillEqually;
        self.stackView.axis = UILayoutConstraintAxisHorizontal;
        self.stackView.spacing = 5;
       
        self.bookDouziView.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap1 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleBookDAction)];
        [self.bookDouziView addGestureRecognizer:tap1];
        
        self.bookRmbView.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDollerDAction)];
        [self.bookRmbView addGestureRecognizer:tap2];
        
        self.bookQuanView.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleYHQDAction)];
        [self.bookQuanView addGestureRecognizer:tap3];
        
        
        self.qdBtn = [AYTool createButtonWithFrame:CGRectZero andTitle:@"" andFont:nil andTitleColor:nil andBg:nil andImage:nil andBgImage:YImage(@"icon_qiand.png") andTarget:self andAction:@selector(qiandaoWithAction) andRadius:0 andAddSubView:self];
        [self.qdBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.equalTo(self.my_txImageView.mas_bottom).offset( YNUM(-8));
            make.right.equalTo(self).offset( YNUM(-16));
            make.height.mas_equalTo(YNUM(34));
            make.width.mas_equalTo(YNUM(71));
        }];
    }
}

- (void)copAction {
    UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
    [pasteboard setString:kStringIsEmpty(_infoModel.inviteCode)?@"":_infoModel.inviteCode];
    ShowToast(@"复制成功");
}

- (void)setConfigModel:(ConfigModel *)configModel {
    _configModel = configModel;
}

- (void)setInfoModel:(UserInfo *)infoModel {
    _infoModel = infoModel;
    [self.my_txImageView sd_setImageWithURL:[NSURL URLWithString:kStringUtils(_infoModel.avatar)] placeholderImage:YImage(@"icon_my_tx.png")];
    self.topLabel.text = kStringUtils(_infoModel.nickName);
    self.downLabel.text = [NSString stringWithFormat:@"邀请码 %@", _infoModel.inviteCode];
    if (kStringIsEmpty(infoModel.beanNum)) {
        self.bookDouziView.topLabel.text = @"0";
    }else {
        self.bookDouziView.topLabel.text = [NSString stringWithFormat:@"%d",infoModel.beanNum.intValue];
    }
    
    self.bookRmbView.topLabel.text = kStringUtils(_infoModel.goldNum);
    AY_ReadManager *readManager = [AY_ReadManager sharedInstance];
    [self.changeColorBtn setImage:readManager.isNight ? YImage(@"AY_mine_yejian") : YImage(@"icon_my_changeColor.png") forState:UIControlStateNormal];
}


- (void)settingWithAction {
    AY_BookSettingPage * page = [[AY_BookSettingPage alloc] init];
    page.userInfoModel = self.infoModel;
    [[AYTool viewControllerWith:self].navigationController pushViewController:page animated:YES];
}

#pragma mark - 皮肤
- (void)changeColorWithAction:(UIButton *)btn {
    AY_ReadManager *readManager = [AY_ReadManager sharedInstance];
    readManager.isNight = !readManager.isNight;
    if (readManager.isNight) {
        [self.changeColorBtn setImage:YImage(@"AY_mine_yejian") forState:UIControlStateNormal];
        [[AYTool lastWindow] addSubview:self.huYanView];
    }else {
        [self.changeColorBtn setImage:YImage(@"icon_my_changeColor") forState:UIControlStateNormal];
        for (int i = 0; i < [[AYTool lastWindow].subviews count]; i ++) {
            UIView *view = [AYTool lastWindow].subviews[i];
            if (view.tag == 263476) {
                [view removeFromSuperview];
            }
        }
    }
}

#pragma mark - 书豆

- (void)handleBookDAction {
    AY_BookDPage * vc = [[AY_BookDPage alloc] init];
    [[AYTool viewControllerWith:self].navigationController pushViewController:vc animated:YES];
}

#pragma mark - 金币

- (void)handleDollerDAction {
    AY_WebViewController *webVC = [[AY_WebViewController alloc] init];
    webVC.fullPath = [NSString stringWithFormat:@"%@%@",self.configModel.web_url,self.configModel.gold_exchange];
    [[AYTool viewControllerWith:self].navigationController pushViewController:webVC animated:YES];
}


#pragma mark - 优惠券

- (void)handleYHQDAction {
    
}



#pragma mark - 签到

- (void)qiandaoWithAction {
    AY_BookQianDPage * vc = [[AY_BookQianDPage alloc] init];
    [[AYTool viewControllerWith:self].navigationController pushViewController:vc animated:YES];
}

- (UIView *)huYanView
{
    if (!_huYanView) {
        _huYanView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
        _huYanView.backgroundColor = [UIColor blackColor];
        _huYanView.alpha = 0.3;
        _huYanView.userInteractionEnabled = NO;
        _huYanView.tag = 263476;
    }
    return _huYanView;
}
@end
